package com.atguigu.common.handler;

import com.atguigu.common.execption.GuiguException;
import com.atguigu.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Jason Gong
 * @version 1.0
 * @Date 2025/2/25
 * @Website https://qingling.icu
 * @Description 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<String> error(Exception e){
        e.printStackTrace();
        return Result.fail(e.getMessage()).message("全局异常");
    }

    /**
     * 特定异常处理
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result<String> error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail(e.getMessage()).message("特定异常");
    }

    /**
     * 自定义异常处理-需手动抛出
     */
    @ExceptionHandler(GuiguException.class)
    @ResponseBody
    public Result<String> error(GuiguException e){
        e.printStackTrace();
        return Result.fail(e.getMessage()).message("自定义异常");
    }
}
